{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import defaultdict\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建哈希表：根据字符ASCII码计数\n",
    "def algorithm1(s, t):                   # 输入两个字符串\n",
    "    record = [0]*26                     # 建立空的计数表（因为小写英文字符26个）\n",
    "    for i in s:\n",
    "        record[ord(i) - ord('a')] +=1   # 英文字符ASCII码是连续顺序编码，以此当前字符减去a第一个的编码就是一个从0开始的索引\n",
    "    for j in t:\n",
    "        record[ord(j) - ord('a')] -=1   \n",
    "    for n in record:                    # 检查s和t的字符异位词，因为如果满足那么record是一个全为零的数组，即两两相消了\n",
    "        if n!=0:\n",
    "            return False                # 存在非零，说明要不就是s要么就是t中有多余或不同的字符存在\n",
    "    return True\n",
    "\n",
    "#库函数调用\n",
    "def algorithm2(s, t):\n",
    "    s_dict = defaultdict(int)           # defaultdict是一个特殊的字典：在访问不存在的key键时，会自动创建键值配对\n",
    "    t_dict = defaultdict(int)\n",
    "    for x in s:\n",
    "        s_dict[x] +=1\n",
    "    for x in t:\n",
    "        t_dict[x] +=1\n",
    "    return s_dict == t_dict\n",
    "\n",
    "def algorithm3(s, t):\n",
    "    s_count = Counter(s)               # counter输入一个字符串时，会返回一个关于各个字母的频率字典\n",
    "    t_count = Counter(t)\n",
    "    return s_count == t_count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(False, False, False)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1 = 'cat'\n",
    "t1 = 'dog'\n",
    "algorithm1(s1,t1), algorithm2(s1,t1), algorithm3(s1,t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(True, True, True)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = 'aabbcc'\n",
    "t2 = 'ccbbaa'\n",
    "algorithm1(s2,t2), algorithm2(s2,t2), algorithm3(s2,t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#对库函数defaultdict和counter理解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(int, {})"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#defaultdict的调用\n",
    "example = 'aabbcc'\n",
    "ep_dict = defaultdict(int)\n",
    "ep_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(int, {'a': 2, 'b': 2, 'c': 2})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for x in example:\n",
    "    ep_dict[x] +=1\n",
    "ep_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({'a': 2, 'b': 2, 'c': 2})"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Counter的调用\n",
    "Counter(example)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
